package com.foreverwin.mes.meapi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.meapi.mapper.ShopOrderMapper;
import com.foreverwin.mes.meapi.model.Item;
import com.foreverwin.mes.meapi.model.ShopOrder;
import com.foreverwin.mes.meapi.service.ItemService;
import com.foreverwin.mes.meapi.service.ShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Syngna
 * @since 2020-02-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {


    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ItemService itemService;

    @Override
    public Page<ShopOrder> selectPage(FrontPage<ShopOrder> frontPage, ShopOrder shopOrder) {
        EntityWrapper<ShopOrder> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(shopOrder);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<ShopOrder> selectList(ShopOrder shopOrder) {
        EntityWrapper<ShopOrder> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(shopOrder);
        return super.selectList(queryWrapper);
    }

    @Override
    public String getBomBo(String shopOrderBo) {
        ShopOrder shopOrderDo = selectById(shopOrderBo);
        if (shopOrderDo.getBomBo() != null) {
            return shopOrderDo.getBomBo();
        }
        if (shopOrderDo.getPlannedBomBo() != null) {
            return shopOrderDo.getPlannedBomBo();
        }
        String itemBo = shopOrderDo.getItemBo();
        if (itemBo == null) {
            itemBo = shopOrderDo.getPlannedItemBo();
        }
        Item itemDo = itemService.selectById(itemBo);
        return itemDo.getBomBo();
    }

}